#include <string.h>
#include <stdlib.h>

#include "entities.h"

static const char *named_mime[][2] =
{
	{ "application/fractals", ".fif" },
	{ "application/futuresplash", ".spl" },
	{ "application/hta", ".hta" },
	{ "application/mac-binhex40", ".hqx" },
	{ "application/ms-vsi", ".vsi" },
	{ "application/msaccess", ".accdb" },
	{ "application/msaccess.addin", ".accda" },
	{ "application/msaccess.cab", ".accdc" },
	{ "application/msaccess.exec", ".accde" },
	{ "application/msaccess.ftemplate", ".accft" },
	{ "application/msaccess.runtime", ".accdr" },
	{ "application/msaccess.template", ".accdt" },
	{ "application/msaccess.webapplication", ".accdw" },
	{ "application/msword", ".doc" },
	{ "application/opensearchdescription+xml", ".osdx" },
	{ "application/pdf", ".pdf" },
	{ "application/pkcs10", ".p10" },
	{ "application/pkcs7-mime", ".p7c" },
	{ "application/pkcs7-signature", ".p7s" },
	{ "application/pkix-cert", ".cer" },
	{ "application/pkix-crl", ".crl" },
	{ "application/postscript", ".ps" },
	{ "application/sdp", ".sdp" },
	{ "application/vnd.fdf", ".fdf" },
	{ "application/vnd.groove-injector", ".grv" },
	{ "application/vnd.groove-space-archive", ".gsa" },
	{ "application/vnd.groove-tool-archive", ".gta" },
	{ "application/vnd.groove-vcard", ".vcg" },
	{ "application/vnd.ms-excel", ".xls" },
	{ "application/vnd.ms-excel.12", ".xlsx" },
	{ "application/vnd.ms-excel.addin.macroEnabled.12", ".xlam" },
	{ "application/vnd.ms-excel.sheet.binary.macroEnabled.12", ".xlsb" },
	{ "application/vnd.ms-excel.sheet.macroEnabled.12", ".xlsm" },
	{ "application/vnd.ms-excel.template.macroEnabled.12", ".xltm" },
	{ "application/vnd.ms-mediapackage", ".mpf" },
	{ "application/vnd.ms-officetheme", ".thmx" },
	{ "application/vnd.ms-package.relationships+xml", ".rels" },
	{ "application/vnd.ms-pki.certstore", ".sst" },
	{ "application/vnd.ms-pki.pko", ".pko" },
	{ "application/vnd.ms-pki.seccat", ".cat" },
	{ "application/vnd.ms-pki.stl", ".stl" },
	{ "application/vnd.ms-powerpoint", ".ppt" },
	{ "application/vnd.ms-powerpoint.12", ".pptx" },
	{ "application/vnd.ms-powerpoint.addin.macroEnabled.12", ".ppam" },
	{ "application/vnd.ms-powerpoint.presentation.macroEnabled.12", ".pptm" },
	{ "application/vnd.ms-powerpoint.slide.macroEnabled.12", ".sldm" },
	{ "application/vnd.ms-powerpoint.slideshow.macroEnabled.12", ".ppsm" },
	{ "application/vnd.ms-powerpoint.template.macroEnabled.12", ".potm" },
	{ "application/vnd.ms-word.document.12", ".docx" },
	{ "application/vnd.ms-word.document.macroEnabled.12", ".docm" },
	{ "application/vnd.ms-word.template.12", ".dotx" },
	{ "application/vnd.ms-word.template.macroEnabled.12", ".dotm" },
	{ "application/vnd.ms-wpl", ".wpl" },
	{ "application/vnd.ms-xpsdocument", ".xps" },
	{ "application/vnd.oasis.opendocument.presentation", ".odp" },
	{ "application/vnd.oasis.opendocument.spreadsheet", ".ods" },
	{ "application/vnd.oasis.opendocument.text", ".odt" },
	{ "application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx" },
	{ "application/vnd.openxmlformats-officedocument.presentationml.slide", ".sldx" },
	{ "application/vnd.openxmlformats-officedocument.presentationml.slideshow", ".ppsx" },
	{ "application/vnd.openxmlformats-officedocument.presentationml.template", ".potx" },
	{ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx" },
	{ "application/vnd.openxmlformats-officedocument.spreadsheetml.template", ".xltx" },
	{ "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".docx" },
	{ "application/vnd.openxmlformats-officedocument.wordprocessingml.template", ".dotx" },
	{ "application/x-bittorrent", ".torrent" },
	{ "application/x-bittorrentsearchdescription+xml", ".btsearch" },
	{ "application/x-compress", ".z" },
	{ "application/x-compressed", ".tgz" },
	{ "application/x-gzip", ".gz" },
	{ "application/x-java-jnlp-file", ".jnlp" },
	{ "application/x-jtx+xps", ".jtx" },
	{ "application/x-latex", ".latex" },
	{ "application/x-mix-transfer", ".nix" },
	{ "application/x-mplayer2", ".asx" },
	{ "application/x-ms-application", ".application" },
	{ "application/x-ms-license", ".slupkg-ms" },
	{ "application/x-ms-vsto", ".vsto" },
	{ "application/x-ms-wmd", ".wmd" },
	{ "application/x-ms-wmz", ".wmz" },
	{ "application/x-ms-xbap", ".xbap" },
	{ "application/x-msexcel", ".xls" },
	{ "application/x-mspowerpoint", ".ppt" },
	{ "application/x-mspowerpoint.12", ".pptx" },
	{ "application/x-mspowerpoint.macroEnabled.12", ".pptm" },
	{ "application/x-mswebsite", ".website" },
	{ "application/x-pkcs12", ".p12" },
	{ "application/x-pkcs7-certificates", ".p7b" },
	{ "application/x-pkcs7-certreqresp", ".p7r" },
	{ "application/x-quicktimeplayer", ".qtl" },
	{ "application/x-rtsp", ".rtsp" },
	{ "application/x-sdp", ".sdp" },
	{ "application/x-shockwave-flash", ".swf" },
	{ "application/x-skype", ".skype" },
	{ "application/x-sparc", ".sparc" },
	{ "application/x-stuffit", ".sit" },
	{ "application/x-tar", ".tar" },
	{ "application/x-troff-man", ".man" },
	{ "application/x-wmplayer", ".asx" },
	{ "application/x-x509-ca-cert", ".cer" },
	{ "application/x-zip-compressed", ".zip" },
	{ "application/xaml+xml", ".xaml" },
	{ "application/xhtml+xml", ".xht" },
	{ "application/xml", ".xml" },
	{ "application/zip", ".zip" },
	{ "applicazione/x-mpeg", ".amc" },
	{ "audio/3gpp", ".3gp" },
	{ "audio/3gpp2", ".3g2" },
	{ "audio/aac", ".aac" },
	{ "audio/ac3", ".AC3" },
	{ "audio/aiff", ".aiff" },
	{ "audio/AMR", ".AMR" },
	{ "audio/basic", ".au" },
	{ "audio/mid", ".mid" },
	{ "audio/midi", ".mid" },
	{ "audio/mp3", ".mp3" },
	{ "audio/mp4", ".m4a" },
	{ "audio/mpeg", ".mp3" },
	{ "audio/mpegurl", ".m3u" },
	{ "audio/mpg", ".mp3" },
	{ "audio/vnd.dlna.adts", ".adts" },
	{ "audio/vnd.qcelp", ".qcp" },
	{ "audio/wav", ".wav" },
	{ "audio/x-aac", ".aac" },
	{ "audio/x-ac3", ".AC3" },
	{ "audio/x-aiff", ".aiff" },
	{ "audio/x-caf", ".caf" },
	{ "audio/x-gsm", ".gsm" },
	{ "audio/x-m4a", ".m4a" },
	{ "audio/x-m4b", ".m4b" },
	{ "audio/x-m4p", ".m4p" },
	{ "audio/x-mid", ".mid" },
	{ "audio/x-midi", ".mid" },
	{ "audio/x-mp3", ".mp3" },
	{ "audio/x-mpeg", ".mp3" },
	{ "audio/x-mpegurl", ".m3u" },
	{ "audio/x-mpg", ".mp3" },
	{ "audio/x-ms-wax", ".wax" },
	{ "audio/x-ms-wma", ".wma" },
	{ "audio/x-wav", ".wav" },
	{ "image/bmp", ".bmp" },
	{ "image/gif", ".gif" },
	{ "image/jp2", ".jp2" },
	{ "image/jpeg", ".jpg" },
	{ "image/jpeg2000", ".jp2" },
	{ "image/jpeg2000-image", ".jp2" },
	{ "image/pict", ".pict" },
	{ "image/pjpeg", ".jpg" },
	{ "image/png", ".png" },
	{ "image/svg+xml", ".svg" },
	{ "image/tiff", ".tiff" },
	{ "image/vnd.ms-photo", ".wdp" },
	{ "image/x-emf", ".emf" },
	{ "image/x-icon", ".ico" },
	{ "image/x-jpeg2000-image", ".jp2" },
	{ "image/x-macpaint", ".pntg" },
	{ "image/x-pict", ".pict" },
	{ "image/x-png", ".png" },
	{ "image/x-quicktime", ".qtif" },
	{ "image/x-sgi", ".sgi" },
	{ "image/x-targa", ".targa" },
	{ "image/x-wmf", ".wmf" },
	{ "midi/mid", ".mid" },
	{ "model/vnd.dwfx+xps", ".dwfx" },
	{ "model/vnd.easmx+xps", ".easmx" },
	{ "model/vnd.edrwx+xps", ".edrwx" },
	{ "model/vnd.eprtx+xps", ".eprtx" },
	{ "pkcs10", ".p10" },
	{ "pkcs7-mime", ".p7m" },
	{ "pkcs7-signature", ".p7s" },
	{ "pkix-cert", ".cer" },
	{ "pkix-crl", ".crl" },
	{ "text/css", ".css" },
	{ "text/html", ".htm" },
	{ "text/plain", ".txt" },
	{ "text/scriptlet", ".wsc" },
	{ "text/vnd-ms.click2record+xml", ".c2r" },
	{ "text/x-component", ".htc" },
	{ "text/x-ms-contact", ".contact" },
	{ "text/x-ms-iqy", ".iqy" },
	{ "text/x-ms-odc", ".odc" },
	{ "text/x-ms-rqy", ".rqy" },
	{ "text/x-vcard", ".vcf" },
	{ "text/xml", ".xml" },
	{ "video/3gpp", ".3gp" },
	{ "video/3gpp2", ".3g2" },
	{ "video/avi", ".avi" },
	{ "video/flc", ".flc" },
	{ "video/mp4", ".mp4" },
	{ "video/mpeg", ".mpeg" },
	{ "video/mpg", ".mpeg" },
	{ "video/msvideo", ".avi" },
	{ "video/quicktime", ".mov" },
	{ "video/sd-video", ".sdv" },
	{ "video/vnd.dlna.mpeg-tts", ".tts" },
	{ "video/x-m4v", ".m4v" },
	{ "video/x-mpeg", ".mpeg" },
	{ "video/x-mpeg2a", ".mpeg" },
	{ "video/x-ms-asf", ".asx" },
	{ "video/x-ms-asf-plugin", ".asx" },
	{ "video/x-ms-wm", ".wmv" },
	{ "video/x-ms-wmv", ".wmv" },
	{ "video/x-ms-wmx", ".wmx" },
	{ "video/x-ms-wvx", ".wvx" },
	{ "video/x-msvideo", ".avi" },
	{ "vnd.ms-pki.certstore", ".sst" },
	{ "vnd.ms-pki.pko", ".pko" },
	{ "vnd.ms-pki.seccat", ".cat" },
	{ "vnd.ms-pki.stl", ".stl" },
	{ "x-pkcs12", ".p12" },
	{ "x-pkcs7-certificates", ".p7b" },
	{ "x-pkcs7-certreqresp", ".p7r" },
	{ "x-x509-ca-cert", ".cer" }
};

static int cmp(const void *key, const void *element)
{
	return strncmp((const char *)key, *(const char **)element,
		strlen(*(const char **)element));
}

const char *mime2ext(const char *name)
{
	const char **entity = (const char**)bsearch(name, named_mime,
		sizeof(named_mime) / sizeof(*named_mime),
		sizeof(*named_mime), cmp);
	return entity ? entity[1] : NULL;
}
